<?php
// +----------------------------------------------------------------------
// | Author: guanxu <510104561@qq.com>
// +----------------------------------------------------------------------
namespace App\Http\Controllers\Api;

use App\Utils\CodeUtil;
use App\Utils\ConstantUtil;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Article;
class ArticleController extends Controller
{
    /**
     * 显示资源列表
     */
    public function index(Request $request)
    {
        $select['id'] = $request->input("id");
        $page = $request->input("page",1);
        $list = Article::orderBy("id","desc");
        if (!empty($select['id']))  $list->where("id",$select['id']);
        $num = ConstantUtil::PAGE_SHOW;
        $list = $list->paginate($num);
        $count = $list->total();
        foreach($list as &$lists) {
            $lists["xh"]        = $count - ($page-1)*$num;
            $count--;
        }
        return view('Api/article/index',["list"=>$list,"select"=>$select]);
    }

    /**
     * 显示创建资源表单页.
     */
    public function create()
    {
        return view('Api/article/create');
    }

    /**
     * 保存新建的资源
     */
    public function save(Request $request)
    {
        $input = $request->input();
        $obj = new Article();
        foreach($input as $key => $val) {
            if($key == "file")  continue;
            if($key == 'file_one') {
                if(empty($val)) return response()->json(['code'=> CodeUtil::API_ERROR,"msg"=> "请上传图片!"]);;
                $obj->img = $val;
                continue;
            }
            $obj->$key = $val;
        }
        $result = $obj->save();
        if ($result) {
            return response()->json(['code'=> CodeUtil::SUCCESS,"msg"=> "操作成功!"]);
        } else {
            return response()->json(['code'=> CodeUtil::API_ERROR,"msg"=> "操作失败!"]);
        }
    }

    /**
     * 显示指定的资源
     */
    public function read(Request $request)
    {
        $list = Article::where("id",$request->input("id"))->first();
        if(empty($list))  return response()->json(['code'=> CodeUtil::API_ERROR,"msg"=> "数据不存在!"]);
        return view('Api/article/read',["list"=>$list]);
    }

    /**
     * 显示编辑资源表单页.
     */
    public function edit(Request $request)
    {
        $id = $request->input("id");
        $list = Article::find($id);
        if(empty($list)) return response()->view("errors::数据不存在");
        return view('Api/article/edit',[
            'list'  => $list,
        ]);
    }

    /**
     * 保存更新的资源
     */
    public function update(Request $request)
    {
        $id = $request->input("id");
        $input = $request->input();
        $obj = Article::find($id);
        foreach($input as $key => $val) {
            if($key == 'id') continue;
            if($key == "file")  continue;
            if($key == 'file_one') {
            if(empty($val)) return response()->json(['code'=> CodeUtil::API_ERROR,"msg"=> "请上传图片!"]);;
                $obj->img = $val;
                continue;
            }
            $obj->$key = $val;
        }
        $obj->save();
        return response()->json(['code'=> CodeUtil::SUCCESS,"msg"=> "操作成功!"]);
    }

    /**
     * 删除指定资源
     */
    public function delete(Request $request)
    {
        $id  = $request->input("id");
        if (!is_array($id)) $id = [$id];
        if(empty($id))  return response()->view("errors::数据不存在");
        Article::whereIn("id",$id)->delete();
        return response()->json(['code'=> CodeUtil::SUCCESS,"msg"=> "删除成功!"]);
    }

    /**
    * 保存更新的资源
    */
    public function change(Request $request)
    {
        $id = $request->input("id");
        $status = $request->input("status");
        Article::where("id",$id)->update(["status"=>$status]);
        return response()->json(['code'=> CodeUtil::SUCCESS,"msg"=> "操作成功!"]);
    }


}
